﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Sdari.Api.DbModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sdari.Api.Gateway.Controllers
{
    [Authorize/*(policy:"Admin")*/]
    public class ApiInfoController : Controller
    {
        private readonly ITokenServiceContext _db;
        private readonly ILogger<ApiInfoController> _logger;

        public ApiInfoController(ITokenServiceContext context, ILogger<ApiInfoController> logger)
        {
            _db = context;
            _logger = logger;
        }

        // GET: ApiInfoes
        public async Task<IActionResult> Index()
        {
            return View(await _db.ApiInfo.ToListAsync());
        }

        // GET: ApiInfoes/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var apiInfo = await _db.ApiInfo
                .FirstOrDefaultAsync(m => m.Id == id);
            if (apiInfo == null)
            {
                return NotFound();
            }

            return View(apiInfo);
        }

        // GET: ApiInfoes/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: ApiInfoes/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Name,Address,Help,Description")] ApiInfo apiInfo)
        {
            if (ModelState.IsValid)
            {
                apiInfo.Uid = Guid.NewGuid();
                apiInfo.CreateDate = DateTime.Now;
                apiInfo.Creator = User.FindFirst("work_no")?.Value;
                //apiInfo.Creator = TempStatics.Modifier;
                _db.Add(apiInfo);
                await _db.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(apiInfo);
        }

        // GET: ApiInfoes/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var apiInfo = await _db.ApiInfo.FindAsync(id);
            if (apiInfo == null)
            {
                return NotFound();
            }
            return View(apiInfo);
        }

        // POST: ApiInfoes/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Uid,Name,Address,Help,Description")] ApiInfo apiInfo)
        {
            if (id != apiInfo.Id)
            {
                return NotFound();
            }
            if (ModelState.IsValid)
            {
                try
                {
                    _db.Attach(apiInfo);
                    var en = _db.Entry(apiInfo);
                    en.Property(nameof(apiInfo.Name)).IsModified = true;
                    en.Property(nameof(apiInfo.Address)).IsModified = true;
                    en.Property(nameof(apiInfo.Help)).IsModified = true;
                    en.Property(nameof(apiInfo.Description)).IsModified = true;
                    apiInfo.LastUpdatedDate = DateTime.Now;
                    apiInfo.LastUpdater = User.FindFirst("work_no")?.Value;
                    //_context.Update(apiInfo);
                    await _db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ApiInfoExists(apiInfo.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(apiInfo);
        }

        // GET: ApiInfoes/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var apiInfo = await _db.ApiInfo
                .FirstOrDefaultAsync(m => m.Id == id);
            if (apiInfo == null)
            {
                return NotFound();
            }

            return View(apiInfo);
        }

        // POST: ApiInfoes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var apiInfo = await _db.ApiInfo.FindAsync(id);
            _db.ApiInfo.Remove(apiInfo);
            await _db.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ApiInfoExists(int id)
        {
            return _db.ApiInfo.Any(e => e.Id == id);
        }
    }
}
